﻿<cfsilent>

	<cfscript>

		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		
		mathAdvice = getProperty("serviceFactory").getBean("senateMathAdvice");
		taskProp = getProperty("serviceFactory").getBean("taskPropertyAdvice");
		sequenceTaskTimeTable = getProperty("serviceFactory").getBean("sequenceTaskTimeTable");
		sequenceTeacherTimeRable =  getProperty("serviceFactory").getBean("sequenceTeacherTimeTable");
		
		response = structNew();
		structInsert(response, "success", false, true);

		taskId = event.getArg("TSK");
		taskType = event.getArg("TT");

		weekDay = event.getArg("wkday");
		lectureId = event.getArg("lec");
		cells = event.getArg("n");

		cellStart = mathAdvice.convertLectureIdxToCellStart(lectureId);
		cellEnd = cellStart + cells;
	
		transaction {
			
			/* 载入教学任务 */
			switch(taskType){
				case 'P':
					sql = "SELECT  a.task_week,a.term_id,
								   a.tch_id,a.cid,
								   a.task_prop,a.task_time 
							FROM t_task a 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'C':
					sql = "SELECT  a.task_week,a.term_id,
								   a.tch_id,a.cid,
								   a.task_prop,a.task_time 
							FROM t_common_task a 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'R':
					sql = "SELECT  a.task_week,a.term_id,
								   a.tch_id,a.cid,
								   a.task_prop,a.task_time 
							FROM t_reopen_task a 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'T':
					sql = "SELECT  a.task_week,a.term_id,
								   a.tch_id,a.cid,
								   a.task_prop,a.task_time 
							FROM t_sport_task a 
							WHERE a.tsk_id = :taskId ";
					break;
			}
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			rs_task = queryObj.execute(sql = sql).getResult();
			
			/* 载入排课任务 */
			sql = "SELECT  a.task_id,a.period_done,
						   a.period_need 
					FROM t_task_arrange a 
					WHERE a.task_id = :taskId ";
					
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			rs_taskArrange = queryObj.execute(sql = sql).getResult();
			
			/* 计算周次序列和已排周学时 */
			taskWeek = listToArray(rs_task.task_week, "-");
			periodWeeks = int(taskWeek[2]) - int(taskWeek[1]) + 1;
			
			weekIdx = mathAdvice.convertWeekIdx(rs_task.task_week);
						
			/* 生成新的时间序列 */
			sequenceTaskTimeTable.setTaskID(rs_taskArrange.task_id);
			timeIdx = sequenceTaskTimeTable.getTimeIDX();
			sequenceTaskTimeTable.setTimeIndex(timeIdx);
			timeId = sequenceTaskTimeTable.getID();
			
			sql = "INSERT INTO t_task_timetable (
							   task_id,
							   widx,
							   wdidx,
							   cellidx,
							   cellrelay,
							   tasktime,
							   task_week,
							   tasktime_id,
							   tidx
					) VALUES (
								:TaskArrange ,
								:WeekIndex ,
								:WeekDay ,
								:CellStart ,
								:CellLength ,
								:TaskClassTime ,
								:TaskWeek ,
								:TaskTimeID ,
								:TimeIndex 
					)  "; 
					
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="TaskArrange", value=rs_taskArrange.task_id, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="WeekIndex", value=weekIdx, cfsqltype="cf_sql_char");
			queryObj.addParam(name ="WeekDay", value=weekDay, cfsqltype="cf_sql_char");
			queryObj.addParam(name ="CellStart", value=cellStart, cfsqltype="cf_sql_interger");
			queryObj.addParam(name ="CellLength", value=cells, cfsqltype="cf_sql_interger");
			queryObj.addParam(name ="TaskClassTime", value=weekDay &"@"& lectureId & "-" & cells, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="TaskWeek", value=rs_task.task_week, cfsqltype="cf_sql_char");
			queryObj.addParam(name ="TaskTimeID", value=timeId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="TimeIndex", value=timeIdx, cfsqltype="cf_sql_char");
			queryObj.execute(sql = sql);
			
			/* 生成教师时间占用表 */
			sequenceTeacherTimeRable.setTermID(rs_task.term_id);
			sequenceTeacherTimeRable.setTeacherID(rs_task.tch_id );
			tchTimeIdx = sequenceTeacherTimeRable.getTimeIDX();
			sequenceTeacherTimeRable.setTimeIndex(tchTimeIdx);
			tchTimeId = sequenceTeacherTimeRable.getID();
			
			sql = "INSERT INTO t_teacher_timetable (
							   widx,
							   wdidx,
							   cellidx,
							   cellrelay,
							   task_id,
							   task_type,
							   term_id,
							   tch_id,
							   cid,
							   task_week,
							   tasktime,
							   teachertime_id,
							   tidx
					) VALUES (
								:WeekIndex ,
								:WeekDay ,
								:CellStart ,
								:CellLength ,
								:TaskID ,
								:TaskType ,
								:Term ,
								:Teacher ,
								:Course ,
								:TaskWeek ,
								:TaskClassTime ,
								:TeacherTimeID ,
								:TimeIndex 
					)  "; 
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="WeekIndex", value=weekIdx, cfsqltype="cf_sql_char");
			queryObj.addParam(name ="WeekDay", value=weekDay, cfsqltype="cf_sql_char");
			queryObj.addParam(name ="CellStart", value=cellStart, cfsqltype="cf_sql_interger");
			queryObj.addParam(name ="CellLength", value=cells, cfsqltype="cf_sql_interger");
			queryObj.addParam(name ="TaskID", value=taskId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="TaskType", value=taskType, cfsqltype="cf_sql_char");
			queryObj.addParam(name ="Term", value=rs_task.term_id, cfsqltype="cf_sql_char");
			queryObj.addParam(name ="Teacher", value=rs_task.tch_id, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="Course", value=rs_task.cid, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="TaskWeek", value=rs_task.task_week, cfsqltype="cf_sql_char");
			queryObj.addParam(name ="TaskClassTime", value=weekDay &"@"& lectureId & "-" & cells, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="TeacherTimeID", value=tchTimeId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="TimeIndex", value=tchTimeIdx, cfsqltype="cf_sql_char");
			queryObj.execute(sql = sql);
			/* 累计已排周学时 */
			periodDone = rs_taskArrange.period_done + periodWeeks * cells;

			sql = "UPDATE t_task_arrange a 
					SET a.period_done = : periodDone 
					WHERE a.task_id = :taskId ";
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="periodDone", value=periodDone, cfsqltype="cf_sql_interger");
			queryObj.execute(sql = sql);
			
			sql = "SELECT  a.task_id,a.period_done,
						   a.period_need 
					FROM t_task_arrange a 
					WHERE a.task_id = :taskId ";
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			rs_taskArrangeNow = queryObj.execute(sql = sql).getResult();
			
			TaskProps = rs_task.task_prop;
			/* 如果已排学时已满足排课要求 则锁定排课任务 */
			if (rs_taskArrangeNow.period_done gte rs_taskArrangeNow.period_need) {
					
				taskProp.parseProperty( rs_task.task_prop );
				taskProp.setArrangeDone(true);
					
				TaskProps = taskProp.stringProperty();
			}
			
						
			/* 保存已安排时间 */
			taskTime = listAppend(rs_task.task_time, weekDay &"@"& lectureId & "-" & cells, ",");
			
			switch(taskType){
				case 'P':
					sql = "UPDATE t_task a 
							SET a.task_prop = :TaskProps ,
								a.task_time = :taskTime 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'C':
					sql = "UPDATE t_common_task a 
							SET a.task_prop = :TaskProps ,
								a.task_time = :taskTime 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'R':
					sql = "UPDATE t_reopen_task a 
							SET a.task_prop = :TaskProps ,
								a.task_time = :taskTime 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'T':
					sql = "UPDATE t_sport_task a 
							SET a.task_prop = :TaskProps ,
								a.task_time = :taskTime 
							WHERE a.tsk_id = :taskId ";
					break;
			}
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="TaskProps", value=TaskProps, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="taskTime", value=taskTime, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			queryObj.execute(sql = sql);
			
			
			
			structInsert(response, "taskID", taskId, true);
			structInsert(response, "weekDay", weekDay, true);
			structInsert(response, "lecture", lectureId, true);
			structInsert(response, "cells", cells, true);
			structInsert(response, "taskTime", taskTime, true);
			structInsert(response, "taskPlace", "", true);

			structInsert(response, "periodDone", rs_taskArrangeNow.period_done, true);
			structInsert(response, "periodNeed", rs_taskArrangeNow.period_need, true);
			
			structInsert(response, "success", true, true);

			
			
		}
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>